<template>
  <view class="cohabitation cohabitation-add">
    <none-data v-if="cohabitationList.length == 0" />
    <CohabitationItem
      v-for="item in cohabitationList"
      :key="item.id"
      :id="item.id"
      :name="item.inmateName"
      :room="item.accountNumber"
      :item="item"
      @on-click="deleteImplement"
    ></CohabitationItem>
    <view class="tail" v-if="cohabitationList.length !== 0">
      {{ tailText }}
    </view>
    
    <up-modal
      :show="modalShow"
      title="操作确认"
      content="是否删除该同住人信息？"
      :showCancelButton="true"
      @confirm="deleteInmate"
      @cancel="modalShow= false"
    ></up-modal>
    <view class="room-btn">
      <button @click="addJump" class="btn" form-type="submit">添加</button>
    </view>
  </view>
</template>

<script setup>
import { customNav } from "@/utils/customNav";
import { getInmateListApi,
  deleteInmateApi, } from "@/package_kuafu_apartment/pages/api/cohabitation";
import CohabitationItem from "@/package_kuafu_apartment/pages/my/components/cohabitation-item/x.vue";
import { onMounted, ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
const pageData = ref({
  page: 1,
  size: 15,
});
const total = ref(0);
const cohabitationList = ref([]);
const tailText = ref("");
const current = ref("");
const modalShow = ref(false);
function addJump() {
  customNav("cohabitation-add?type=新增");
}
async function getInmateList() {
  let res = await getInmateListApi(pageData.value);
  console.log(res);
  cohabitationList.value = res.data.records;
  total.value = res.data.total;
  if (cohabitationList.value.length !== total.value) {
    tailText.value = "下滑加载更多";
  } else {
    tailText.value = "没有更多内容";
  }
}
function deleteImplement(id) {
  console.log(id);
  current.value = id;
  modalShow.value = true
}
async function deleteInmate () {
  let res =await deleteInmateApi(current.value)
  console.log(res);
  if (res.code == 200) {
    uni.showToast({
      icon: "none",
      title: res.msg,
      duration: 2000,
    });
    modalShow.value = false
    pageData.value.page = 1;
    getInmateList();
  } else {
    uni.showToast({
      icon: "none",
      title: res.msg,
      duration: 2000,
    });
  }
}
onMounted(() => {
  getInmateList();
});
onShow(() => {
  getInmateList();
});
</script>


<style lang="less" scoped>
@import url(./style/cohabitation-add.less);
.cohabitation {
  padding-top: 0;
}
</style>